<?php


function strip_string($Array, $obj = "#") {
    $Result = "";
    if ($Array == "")
        return $Result;
    foreach ($Array as $Row) {
        if ($Row != "") {
            $Result .= $Row . $obj;
        }
    }
    if ($Result != "") {
        $Result = substr($Result, 0, -strlen($obj));
    }
    return $Result;
}

function print_table($Header, $ArrayData, $Columns, $EditButton = false, $RemoveButton = false) {
    $CI = & get_instance();
    $CI->load->library('table');
    $data = array(
        $Header
    );
    $i = 0;
    foreach ($ArrayData as $Row) {

        $data_row = array();

        foreach ($Columns as $Column) {
            $data_row[] = $Row[$Column];
        }
        if ($EditButton)
            $data_row[] = '<div button="edit-btn" class="add-btn" number="' . $i . '">Edit</div>';
        if ($RemoveButton)
            $data_row[] = '<div button="delete-btn" class="add-btn" number="' . $i . '">Remove</div>';
        $i++;
        $data[] = $data_row;
    }
        $tmpl = array (
            'table_open'          => '<table class="list_show">',
         );
                $CI->table->set_template($tmpl); 
    echo $CI->table->generate($data);
}

function ConvtAddQual($Qualifications, $ReturnString = true) {
    if($ReturnString){
        //Strip And First
        $String = "";
        $AndVerbs = explode('&&', $Qualifications);
        foreach ($AndVerbs as $AndVerb) {
            $OrVerbs = explode('||', $AndVerb);
            foreach ($OrVerbs as $OrVerb) {
                $Qual = explode("@@", $OrVerb);
                $String .= $Qual[0] . " ";
                $String .= empty($Qual[1]) ? "" : $Qual[1] . " ";
                $String .= "or ";
            }
            $String = substr($String, 0, -3);
            $String .= "\n";
        }
        return nl2br($String);
    }
    else{
        $Array = array();
        $AndVerbs = explode('&&', $Qualifications);
        $iAnd = 0; $countAnd = count($AndVerbs);
        foreach ($AndVerbs as $AndVerb) {
            $OrVerbs = explode('||', $AndVerb);
            $i = 0; $count = count($OrVerbs);
            foreach ($OrVerbs as $OrVerb) {
                $Qual = explode("@@", $OrVerb);
                $Row = array();
                $Row['ReqName']     = trim($Qual[0]);
                $Row['ReqLevel']    = trim($Qual[1]);
                if($iAnd < ($countAnd - 1) || $i < $count-1){
                    if($i != $count-1)
                        $Row['ReqCon'] = '||';
                    else
                        $Row['ReqCon'] = '&&';
                }
                $i++;
                $Array[] = $Row;       
            }
            $iAnd++;
        }
        return $Array;
    }
}

function QualificationRecognize($ID) {
    switch ($ID) {
        case 1:
            return "Primary";
            break;
        case 2:
            return "Junior High";
            break;
        case 3:
            return "High School";
            break;
        case 4:
            return "College";
            break;
        case 5:
            return "University";
            break;
        case 6:
            return "Bachelor";
            break;
        case 7:
            return "Master";
            break;
        case 8:
            return "Doctor";
            break;
    }  
}
function compare_requirement($AppSkills, $Reqs){
    $Result = array();
    //Loop each Requirement
    $IsOr = 0;
    foreach($Reqs as $Req){
        if($IsOr == 1){
            $Row['ReqName']  .= '<br>'.$Req['ReqName'];
            $Row['ReqLevel'] .= '<br>'.$Req['ReqLevel'];
        }
        else{
            $Row['ReqName']     = $Req['ReqName'];
            $Row['ReqLevel']    = $Req['ReqLevel'];
            $Row['ReqPass']     = 0;
        }

        //Loop each skills to check
        if($IsOr != 1 || $Row['ReqPass'] < 2)
        foreach($AppSkills as $Skill){
            $PassLevel = 0;
            //Explode the Qualification Name + Alias
            $QualNames = explode("#", $Skill['QualificationName']);
            if(in_array($Req['ReqName'], $QualNames)){
                $PassLevel++;
                if($Skill['QualificationLevel'] >= $Req['ReqLevel']){
                    $PassLevel++;
                }
            }
            if($PassLevel > $Row['ReqPass']){
                $Row['ReqPass'] = $PassLevel;
            }
            //If max break the check
            if($PassLevel >= 2){              
                break;
            }
        }
        if(!empty($Req['ReqCon']) && $Req['ReqCon'] == "||")
            $IsOr = 1;
        else{
            $IsOr = 0;
            $Result[] = $Row;
        }

    }
    return $Result;
}
